﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using Superpow;
using EasyMobile;

public class MainController : BaseController {
    public Text hintCostText, pickBoxCostText;

    private int level;
    private bool isGameComplete;
    private GameLevel gameLevel;

    public static MainController instance;

    protected override void Awake()
    {
        base.Awake();
        instance = this;
    }

    protected override void Start()
    {
        base.Start();
        level = Prefs.CurrentLevel;
        GameState.mainCells = new Dictionary<Position, Cell>();

        hintCostText.text = ConfigController.Config.hintCost.ToString();
        pickBoxCostText.text = ConfigController.Config.pickBoxCost.ToString();

        gameLevel = Utils.Load(level);
        Pan.instance.Load(gameLevel);
        WordRegion.instance.Load(gameLevel);

        if (level == 0)
        {
            Timer.Schedule(this, 0.5f, () =>
            {
                DialogController.instance.ShowDialog(DialogType.HowtoPlay);
            });
        }

        Music.instance.ChangeMusic();
    }

    public void OnComplete()
    {
        if (isGameComplete) return;
        isGameComplete = true;

        Timer.Schedule(this, 1f, () =>
        {
            DialogController.instance.ShowDialog(DialogType.Win);
            Sound.instance.Play(Sound.Others.Win);
        });
    }

    public void OnShareClick()
    {
#if (UNITY_IOS || UNITY_ANDROID) && !UNITY_EDITOR
        StartCoroutine(DoShare());
#else
        Toast.instance.ShowMessage("This function only works on Android and iOS");
#endif
    }

    public IEnumerator DoShare()
    {
        yield return new WaitForEndOfFrame();
        Sharing.ShareScreenshot("screenshot", "");
    }

    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.Escape) && !DialogController.instance.IsDialogShowing())
        {
            DialogController.instance.ShowDialog(DialogType.Pause);
        }
    }
}
